package com.xsherl.leetcode.utils;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ArrayUtils {

    public static<T> T parse(String text){
        return JSON.parseObject(text, new TypeReference<T>(){});
    }

    public static<T> T[] parseArray(String text, Class<T> tClass){
        List<T> ts = JSONArray.parseArray(text, tClass);
        return ts.toArray((T[])Array.newInstance(tClass, 0));
    }

    public static<T> T[][] deepParseArray(String text, Class<T> tClass){
        Class<T[]> arrayType = (Class<T[]>)ArrayUtil.getArrayType(tClass);
        return parseArray(text, arrayType);
    }

    public static<T> boolean allMatch(List<T> list1, List<T> list2){
        if (list1.size() != list2.size()){
            return false;
        }
        List<T> list = new ArrayList<>(list1);
        for (T t: list2){
            int i = list.indexOf(t);
            if (i == -1){
                return false;
            }
            list.remove(i);
        }
        return list.isEmpty();
    }

    public static<T> boolean allMatchWithoutOrder(List<List<T>> list1, List<List<T>> list2){
        if (list1.size() != list2.size()){
            return false;
        }
        List<List<T>> list = new ArrayList<>(list1);
        for (List<T> t: list2){
            int i = 0;
            for (;i < list.size(); ++i){
                if (allMatch(t, list2.get(i))){
                    break;
                }
            }
            if (i == list.size()){
                return false;
            }
            list.remove(i);
        }
        return list.isEmpty();
    }


}
